﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Web.UI.WebControls;



public partial class UserControls_Stones : System.Web.UI.UserControl
{
    private IList<DTOS.Products.StonesDTO> lstStones = new List<DTOS.Products.StonesDTO>();


    public IList<DTOS.Products.StonesDTO> GetStones { get { Read(); return lstStones; } }

    //protected void Page_Load(object sender, EventArgs e)
    //{
    //    if (!IsPostBack)
    //        SetInitialRow();
    //}

    //private void SetInitialRow()
    //{
    //    List<DTOS.Products.StonesDTO> lstStones = new List<DTOS.Products.StonesDTO>();
    //    for (int i = 0; i <=1; i++)
    //    {

    //        DTOS.Products.StonesDTO objstones = new DTOS.Products.StonesDTO();
    //        lstStones.Add(objstones);
    //    }
    //    ViewState["vsStoneDtls"] = lstStones;
    //    Gridview1.DataSource = lstStones;
    //    Gridview1.DataBind();

    //}

    //private void SetPreviousData()
    //{
    //    int rowIndex = 0;
    //    if (ViewState["vsStoneDtls"] != null)
    //    {
    //        List<DTOS.Products.StonesDTO> lstStones = (List<DTOS.Products.StonesDTO>)ViewState["vsStoneDtls"];
    //        List<DTOS.Products.StonesDTO> lstnewStones = new List<DTOS.Products.StonesDTO>();

    //        if (lstStones.Count > 0)
    //        {
    //            foreach (DTOS.Products.StonesDTO Stones in lstStones)
    //            {
    //                TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[0].FindControl("TextBox1");
    //                TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox2");
    //                TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox3");
    //                TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox4");
    //                TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox5");
    //                TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox6");
    //                TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox7");
    //                Stones.StoneCode = box1.Text.Trim();
    //                Stones.StoneName = box2.Text.Trim();
    //                Stones.StonesCount = String.IsNullOrEmpty(box3.Text.Trim()) == true ? 0 : Convert.ToInt32(box3.Text.Trim());
    //                Stones.UnitCost = String.IsNullOrEmpty(box4.Text.Trim()) == true ? 0 : Convert.ToDecimal(box4.Text.Trim());
    //                Stones.StonesWeight = String.IsNullOrEmpty(box5.Text.Trim()) == true ? 0 : Convert.ToDecimal(box5.Text.Trim());
    //                Stones.StonesUnit = box6.Text.Trim();
    //                Stones.TotalCost = String.IsNullOrEmpty(box7.Text.Trim()) == true ? 0 : Convert.ToDecimal(box7.Text.Trim());
    //                lstnewStones.Add(Stones);
    //            }
    //            ViewState["vsStoneDtls"] = lstnewStones;
    //            Gridview1.DataSource = lstnewStones;
    //            Gridview1.DataBind();
    //        }
    //    }
    //}

    //private void AddNewRowToGrid()
    //{
    //    //Set Previous Data on Postbacks
    //    SetPreviousData();

    //    if (ViewState["vsStoneDtls"] != null)
    //    {
    //        List<DTOS.Products.StonesDTO> lstStones = (List<DTOS.Products.StonesDTO>)ViewState["vsStoneDtls"];
    //        if (lstStones.Count > 0)
    //        {
    //            DTOS.Products.StonesDTO objstones = new DTOS.Products.StonesDTO();
    //            lstStones.Add(objstones);
    //            ViewState["vsStoneDtls"] = lstStones;
    //            Gridview1.DataSource = lstStones;
    //            Gridview1.DataBind();
    //        }
    //    }
    //    else
    //    {
    //        Response.Write("ViewState is null");
    //    }


    //}

    //protected void ButtonAdd_Click(object sender, EventArgs e)
    //{
    //    AddNewRowToGrid();
    //}

    public List<DTOS.Products.MasterStones> GetMstStones()
    {
        List<DTOS.Products.MasterStones> lst = (new BLL.Concrete.Products.CommonBAL()).GetMasterStones();
        //ddl.Items.Clear();
        //ddl.DataSource = lst;
        //ddl.DataValueField = "STONECODE";
        //ddl.DataTextField = "STONENAME";
        //ddl.DataBind();

        return lst;


    }

    public List<DTOS.Products.UnitsDTO> GetUnits()
    {
        return new BLL.Concrete.Products.CommonBAL().GetMasterUnits();
    }

    private void SetInitialRow()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;
        dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
        dt.Columns.Add(new DataColumn("STONECODE", typeof(string)));
        //dt.Columns.Add(new DataColumn("STONENAME", typeof(string)));
        dt.Columns.Add(new DataColumn("STONESCOUNT", typeof(string)));
        dt.Columns.Add(new DataColumn("UNITCOST", typeof(string)));
        dt.Columns.Add(new DataColumn("STONESWEIGHT", typeof(string)));
        dt.Columns.Add(new DataColumn("STONESUNIT", typeof(string)));
        dt.Columns.Add(new DataColumn("TOTALCOST", typeof(string)));
        dr = dt.NewRow();
        dr["RowNumber"] = 1;
        dr["STONECODE"] = string.Empty;
        // dr["STONENAME"] = string.Empty;
        dr["STONESCOUNT"] = string.Empty;
        dr["UNITCOST"] = string.Empty;
        dr["STONESWEIGHT"] = string.Empty;
        dr["STONESUNIT"] = string.Empty;
        dr["TOTALCOST"] = string.Empty;

        dt.Rows.Add(dr);

        //Store the DataTable in ViewState
        ViewState["CurrentTable"] = dt;

        Gridview1.DataSource = dt;
        Gridview1.DataBind();
    }

    private void AddNewRowToGrid()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    DropDownList ddl = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("ddlStoneNames");
                    // TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    //  TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox3");
                    TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox4");
                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox5");
                    // TextBox box6 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("TextBox6");
                    DropDownList ddlUnits = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("ddlUnits");
                    TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox7");

                    drCurrentRow = dtCurrentTable.NewRow();
                    drCurrentRow["RowNumber"] = i + 1;

                    dtCurrentTable.Rows[i - 1]["STONECODE"] = ddl.SelectedValue;
                    // dtCurrentTable.Rows[i - 1]["STONENAME"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["STONESCOUNT"] = box3.Text;
                    dtCurrentTable.Rows[i - 1]["UNITCOST"] = box4.Text;
                    dtCurrentTable.Rows[i - 1]["STONESWEIGHT"] = box5.Text;
                    dtCurrentTable.Rows[i - 1]["STONESUNIT"] = ddlUnits.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["TOTALCOST"] = box7.Text;

                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
    }

    private void SetPreviousData()
    {
        int rowIndex = 0;
        if (ViewState["CurrentTable"] != null)
        {
            DataTable dt = (DataTable)ViewState["CurrentTable"];
            lstStones.Clear();
            //  GetStones = new List<DTOS.Products.StonesDTO>();
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {

                    DropDownList ddl = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("ddlStoneNames");

                    //TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    // TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox3");
                    TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox4");
                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox5");
                    DropDownList ddlUnits = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("ddlUnits");
                    TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox7");

                    ddl.SelectedValue = dt.Rows[i]["STONECODE"].ToString();
                    //   box2.Text = dt.Rows[i]["STONENAME"].ToString();
                    box3.Text = dt.Rows[i]["STONESCOUNT"].ToString();
                    box4.Text = dt.Rows[i]["UNITCOST"].ToString();
                    box5.Text = dt.Rows[i]["STONESWEIGHT"].ToString();
                    ddlUnits.SelectedValue = dt.Rows[i]["STONESUNIT"].ToString();
                    box7.Text = dt.Rows[i]["TOTALCOST"].ToString();


                    DTOS.Products.StonesDTO Stones = new DTOS.Products.StonesDTO();
                    Stones.StoneCode = ddl.SelectedValue.Trim();
                    // Stones.StoneName = box2.Text.Trim();
                    Stones.StonesCount = String.IsNullOrEmpty(box3.Text.Trim()) == true ? 0 : Convert.ToInt32(box3.Text.Trim());
                    Stones.UnitCost = String.IsNullOrEmpty(box4.Text.Trim()) == true ? 0 : Convert.ToDecimal(box4.Text.Trim());
                    Stones.StonesWeight = String.IsNullOrEmpty(box5.Text.Trim()) == true ? 0 : Convert.ToDecimal(box5.Text.Trim());
                    Stones.StonesUnit = ddlUnits.SelectedValue;
                    Stones.TotalCost = String.IsNullOrEmpty(box7.Text.Trim()) == true ? 0 : Convert.ToDecimal(box7.Text.Trim());
                    lstStones.Add(Stones);


                    rowIndex++;
                }
            }
        }
    }

    public void Read()
    {
        int rowIndex = 0;

        if (ViewState["CurrentTable"] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
            // DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    DropDownList ddl = (DropDownList)Gridview1.Rows[rowIndex].Cells[1].FindControl("ddlStoneNames");
                    // TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
                    //  TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
                    TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox3");
                    TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox4");
                    TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("TextBox5");
                    DropDownList ddlUnits = (DropDownList)Gridview1.Rows[rowIndex].Cells[5].FindControl("ddlUnits");

                    TextBox box7 = (TextBox)Gridview1.Rows[rowIndex].Cells[6].FindControl("TextBox7");

                    // drCurrentRow = dtCurrentTable.NewRow();
                    // drCurrentRow["RowNumber"] = i + 1;

                    dtCurrentTable.Rows[i - 1]["STONECODE"] = ddl.SelectedValue;
                    // dtCurrentTable.Rows[i - 1]["STONENAME"] = box2.Text;
                    dtCurrentTable.Rows[i - 1]["STONESCOUNT"] = box3.Text;
                    dtCurrentTable.Rows[i - 1]["UNITCOST"] = box4.Text;
                    dtCurrentTable.Rows[i - 1]["STONESWEIGHT"] = box5.Text;
                    dtCurrentTable.Rows[i - 1]["STONESUNIT"] = ddlUnits.SelectedValue;
                    dtCurrentTable.Rows[i - 1]["TOTALCOST"] = box7.Text;

                    rowIndex++;
                }
                //dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState["CurrentTable"] = dtCurrentTable;

                Gridview1.DataSource = dtCurrentTable;
                Gridview1.DataBind();
            }
        }
        else
        {
            Response.Write("ViewState is null");
        }

        //Set Previous Data on Postbacks
        SetPreviousData();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            SetInitialRow();
        }
    }

    protected void btnAddRows_Click(object sender, EventArgs e)
    {
        AddNewRowToGrid();
    }

}